package com.example.esdemo.config;

import com.example.esdemo.component.SpringfoxJsonToGsonAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import springfox.documentation.spring.web.json.Json;

/**
 * @author kaibo
 * @date 2019/11/13 10:00
 * @GitHub：https://github.com/yuxuelian
 * @email：kaibo1hao@gmail.com
 * @description：
 */

@Configuration
public class GsonConfig {

    @Bean
    @Primary
    public Gson gson(SpringfoxJsonToGsonAdapter springfoxJsonToGsonAdapter) {
        return new GsonBuilder()
                .registerTypeAdapter(Json.class, springfoxJsonToGsonAdapter)
                .serializeNulls()
                .create();
    }

    @Bean
    @Primary
    public GsonHttpMessageConverter gsonHttpMessageConverter(Gson gson) {
        GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
        converter.setGson(gson);
        return converter;
    }

}
